package com.zoffy.oj_backend.judge.codesandbox.impl.ExampleCodeSandbox.codeExcuter;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.zoffy.oj_backend.judge.codesandbox.model.ExecuteMessage;
import com.zoffy.oj_backend.judge.codesandbox.utils.ProcessUtil;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractCodeExecutor implements CodeExecutor {
    private static final String GLOBAL_CODE_DIR_NAME = "tmpCode";

    protected static final long TIME_OUT = 5000L;

    public abstract File saveCode2File(String code);

    public abstract ExecuteMessage compileCode(File codeFile);

    public abstract List<ExecuteMessage> runCodeFile(File file, List<String> inputList);

    protected File saveCode2File(String code, String fileName) {
        String userDir = System.getProperty("user.dir");
        String globalCodePathName = userDir + File.separator + GLOBAL_CODE_DIR_NAME;
        // 判断全局代码目录是否存在，没有则新建
        if (!FileUtil.exist(globalCodePathName)) {
            FileUtil.mkdir(globalCodePathName);
        }

        // 把用户的代码隔离存放
        String userCodeParentPath = globalCodePathName + File.separator + UUID.randomUUID();
        String userCodePath = userCodeParentPath + File.separator + fileName;
        return FileUtil.writeString(code, userCodePath, StandardCharsets.UTF_8);

    }

    protected ExecuteMessage compileCode(File codeFile, String compileCommand) {
        try {
            Process process = Runtime.getRuntime().exec(compileCommand + codeFile.getPath());
            ExecuteMessage executeMessage = ProcessUtil.runProcessAndGetMessage(process);
//            if (executeMessage.getExitCode() != 0) {
//                throw new RuntimeException("编译错误");
//            }
            return executeMessage;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    protected List<ExecuteMessage> runCodeFile(File file, List<String> inputList, String runningCommand) {
        String parentPath = file.getParentFile().getAbsolutePath();
        List<ExecuteMessage> executeMessageList = new ArrayList<>();
        for (String input : inputList) {
            String runCmd = String.format(runningCommand, parentPath, input);
            try {
                Process process = Runtime.getRuntime().exec(runCmd);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(TIME_OUT);
                            process.destroy();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }

                    }
                }).start();
                ExecuteMessage executeMessage = ProcessUtil.runProcessAndGetMessage(process);
                executeMessageList.add(executeMessage);
                if(executeMessage.getExitCode()!=0){
                    break;
                }
            } catch (Exception e) {
                System.out.println("执行错误");
            }
        }
        return executeMessageList;
    }
}
